<?php
/**
 * Created by PhpStorm.
 * User: Hongjw
 * Date: 2018/3/1
 * Time: 18:10
 */

namespace App\Tools\Common;


class ComCheckCode
{
    const SUCCESS = 1000;
    const FAIL = 1001;

    /**
     * @param $res
     * @return bool
     */
    public static function checkRes($res)
    {
        if ($res['code'] == self::SUCCESS) {
            return true;
        }
        return false;
    }

    /**
     * 检测失败,返回json终止程序
     * @param $res
     */
    public static function failExit($res){
        $resFinal = self::checkRes($res);
        if(!$resFinal){
            echo json_encode($resFinal);
            exit();
        }
    }

    public static function returnRes($code, $msg, $ex = [])
    {
        $res = ['code' => $code, 'msg' => $msg] + $ex;
        return $res;
    }

    public static function successRes($ex = []){
        $res = self::returnRes(self::SUCCESS,'Success',$ex);
        return $res;
    }

    public static function failRes($msg = '',$ex = []){
        $msg = empty($msg) ? "Fail" : $msg;
        $res = self::returnRes(self::FAIL,$msg,$ex);
        return $res;
    }
}